/******************* Copyright(c) HDM  *******************
* 
*------File Info--------------------------------------------------------------
* File Name:            LCD.c
* Latest modified Date: 
* Latest Version:       
* Description:          串口液晶显示屏DC80480B070_03相关函数
* compiler:             MDK V4.73
* MCU:                  STM32F103VE
* Oscillator Crystal Frequency:    8MHz
*-----------------------------------------------------------------------------
* Created By:    朱茂群
* Created date:  2015-08-26
* Version:       v0.1
* Descriptions:  
*
*-----------------------------------------------------------------------------
* Modified by:   修改人的姓名
* Modified date: 文件的修改日期（YYYY-MM-DD）
* Version:       文件修订的版本号
* Description:   文件修订的简要描述
*
******************************************************************************/

/* Includes -----------------------------------------------------------------*/
#include <stdlib.h>
#include "../../BSP/bsp_uart_fifo.h"
#include "stdint.h"
#include "cmsis_os.h"


/* Private typedef ----------------------------------------------------------*/
/* Private define -----------------------------------------------------------*/
#define LCD_SET_PSW (123456)                      //串口显示屏进入设置界面的密码(不能超过9个数字)
/* Private macro ------------------------------------------------------------*/
#define LCD_CMD_BUF_MAX  100
/* Private variables --------------------------------------------------------*/
static char Lcd_CmdBuf[LCD_CMD_BUF_MAX];        //串口显示屏命令保存区
static uint8_t Lcd_CmdBufCnt = 0;               //串口显示屏命令保存区的数据数量
volatile uint8_t LCD_ScreenId = 0;              //串口显示屏当前画面ID,用于更新显示
uint8_t Lcd_ScreenSwitch;


#define LCD_COM COM1



static osMutexId osMutexLcd;            /* LCD接口互斥信号量 */

/* Private function prototypes ----------------------------------------------*/
static void LCD_SendBuf(uint8_t *_ucaBuf, uint16_t _usLen);


/******************************************************************************
  Function:     LCD_Icon
  Description:  设置LCD图标状态
  Input:        uint8_t screen_id,  屏幕ID号0~255
                uint8_t icon_id,    图标ID号0~255
                uint8_t state       指定状态0~255
  Return:       none
  Others:       none
******************************************************************************/
void LCD_Icon(uint8_t screen_id, uint8_t icon_id, uint8_t state)
{
    char buf[12] = { 0xEE, 0xB1, 0x23, 0, 0, 0, 0, 0, 0xFF, 0xFC, 0xFF, 0xFF };
    buf[4] = screen_id;
    buf[6] = icon_id;
    buf[7] = state;
    LCD_SendBuf((uint8_t*)buf, 12);
}

/******************************************************************************
  Function:     LCD_Gif
  Description:  LCD动画控件控制,目前仅支持控制动画播放和停止
  Input:        uint8_t screen_id,  屏幕ID号0~255
                uint8_t control_id, 控件ID号0~255
                uint8_t state       动画状态0:停止; 1:播放
  Return:       none
  Others:       none
******************************************************************************/
void LCD_Gif(uint8_t screen_id, uint8_t control_id, uint8_t state)
{
    char buf[11] = { 0xEE, 0xB1, 0x21, 0, 0, 0, 0, 0xFF, 0xFC, 0xFF, 0xFF };
    if (state == 0)
    {
        buf[2] = 0x21;
    }
    else
    {
        buf[2] = 0x20;
    }

    buf[4] = screen_id;
    buf[6] = control_id;

    LCD_SendBuf((uint8_t *)buf, 11);
}

/******************************************************************************
  Function:     LCD_Txt
  Description:  更新文本控件显示内容
  Input:        uint8_t screen_id,  屏幕ID号0~255
                uint8_t control_id, 控件ID号0~255
                uint8_t str         显示字符串
  Return:       none
  Others:       none
******************************************************************************/
void LCD_Txt(uint8_t screen_id, uint8_t icon_id, char *str)
{
//  char buf[12] = { 0xEE, 0xB1, 0x10, 0, 0, 0, 0 };//应该不需要12那么长
    char buf[7] = { 0xEE, 0xB1, 0x10, 0, 0, 0, 0 };
    const char buf1[] = { 0xFF, 0xFC, 0xFF, 0xFF };
    buf[4] = screen_id;
    buf[6] = icon_id;
  LCD_SendBuf( (uint8_t*)buf, 7);
  LCD_SendBuf((uint8_t*)str,strlen(str));
  LCD_SendBuf( (uint8_t*)buf1, 4);
}

/******************************************************************************
  Function:     LCD_SetScreen
  Description:  设置串口显示屏切换到所需的画面
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void LCD_SetScreen(uint8_t screen_id)
{
    char buf[9] = { 0xEE, 0xB1, 0x00, 0, 0, 0xFF, 0xFC, 0xFF, 0xFF };

    buf[4] = screen_id;
    LCD_ScreenId = screen_id;
    Lcd_ScreenSwitch = 1;                   //界面切换,置位标志位

    LCD_SendBuf((uint8_t*)buf, 9);
}

/******************************************************************************
  Function:     UpdateLcdDisplay
  Description:  更新串口显示屏的显示内容
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void UpdateLcdDisplay(void)
{
//    TODO update display
    switch (LCD_ScreenId)
    {
        default:
            break;
    }
}


/******************************************************************************
  Function:     GetLcdCmd
  Description:  从串口缓冲区中获取一帧串口液晶显示屏命令,存入Lcd_CmdBuf[]
  Input:        none
  Return:       0:      获取失败
                1~255:  获取成功返回命令长度
  Others:       none
******************************************************************************/
uint8_t GetLcdCmd(void)
{
    static uint8_t Lcd_CmdState = 0;                //串口显示屏命令帧接收状态
    uint8_t _data = 0;
	  

    while (comGetChar(LCD_COM, &_data))
    {
        //取一个数据
//      comGetChar(LCD_COM, &_data);

        if (Lcd_CmdBufCnt == 0 && _data != 0xEE) //帧头出错，跳过
            continue;

        //缓存区溢出,缓存区清除,退出
        if (Lcd_CmdBufCnt >= LCD_CMD_BUF_MAX)
        {
            Lcd_CmdBufCnt = 0;
            Lcd_CmdState = 0;
            return 0;
        }
        Lcd_CmdBuf[Lcd_CmdBufCnt++] = _data;

        //判断帧尾
        if (_data == 0xFF)
        {
            switch (Lcd_CmdState)
            {
            case 2:
                Lcd_CmdState = 3;
                break; //FF FC FF ?? (最后一个字节不对)
            case 3:
                Lcd_CmdState = 4;
                break; //FF FC FF FF 正确的帧尾
            default:
                Lcd_CmdState = 1;
                break; //FF ?? ?? ??(最后三个字节不对)
            }
        }
        else if (_data == 0xFC)
        {
            switch (Lcd_CmdState)
            {
            case 1:
                Lcd_CmdState = 2;
                break; //FF FC ?? ??(最后二个字节不对)
            case 3:
                Lcd_CmdState = 2;
                break; //FF FC FF FC 正确的帧尾
            default:
                Lcd_CmdState = 0;
                break; //?? ?? ?? ??(全部字节不对)
            }
        }
        else
            Lcd_CmdState = 0;

        //得到完整的帧尾
        if (Lcd_CmdState == 4)
        {
            Lcd_CmdState = 0;
            Lcd_CmdBufCnt = 0;
            return 1;
        }
    }

    return 0; //没有形成完整的一帧
}

/******************************************************************************
  Function:     LCD_CmdHandle
  Description:  处理串口显示屏的返回的命令
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
uint8_t LCD_CmdHandle(void)
{
    static uint32_t password = 0;                      //保存访问设置界面的密码
    uint16_t response_id;                            //高8位是画面id,低8位是控件id
    char *p;
    char str_tmp[10];
    uint8_t i;
    uint16_t j;
	int temp;
	int year,month,date;

    if (GetLcdCmd() == 0)                              //未接收到串口显示屏命令帧
    {
        return 0;
    }

    //处理命令帧
    response_id = (uint16_t)(Lcd_CmdBuf[4] << 8);     //获取画面id
    response_id |= (uint8_t)Lcd_CmdBuf[6];          //获取控件id
#if 0
//TODO lcd Handler



#endif
    return 1;
}

/******************************************************************************
  Function:     LCD_SendBuf
  Description:  发送信息给串口屏
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
static void LCD_SendBuf(uint8_t *_ucaBuf, uint16_t _usLen)
{
    osMutexWait(osMutexLcd, 0);
    comSendBuf(LCD_COM, _ucaBuf, _usLen);
    osMutexRelease(osMutexLcd);
}

/******************************************************************************
  Function:     LCD_Init
  Description:  串口屏初始化
  Input:        none
  Return:       none
  Others:       none
******************************************************************************/
void LCD_Init(void)
{
    osMutexLcd = osMutexCreate(NULL);
}
/************** (C) COPYRIGHT HDM  ********END OF FILE****/



